// Shader created with Shader Forge v1.40 
// Shader Forge (c) Freya Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.40;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,cgin:,cpap:True,lico:1,lgpr:1,limd:1,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,imps:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,atcv:False,rfrpo:True,rfrpn:Refraction,coma:15,ufog:True,aust:True,igpj:False,qofs:0,qpre:1,rntp:1,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,atwp:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False,fsmp:False;n:type:ShaderForge.SFN_Final,id:2288,x:32719,y:32712,varname:node_2288,prsc:2;pass:END;sub:END;*/

Shader "Unlit/OldSchoolPlus2" {
    Properties {
        [Header(Texture)]
            _MainTex    ("RGB:基础颜色 A:环境遮罩", 2D) =   "white" {}
            _NormTex    ("RGB:法线贴图" , 2D) =             "bump" {}   // 一定要bump,否则美术迷惑
            _SpecTex    ("RGB:高光颜色 A:高光次幂", 2D) =   "gary" {}
            _EmitTex    ("RGB:自发光贴图", 2D) =              "black" {}
            _Cubemap    ("RGB:环境贴图", cube) =            "_Skybox" {}

        [Header(Diffuse)]
            _MainCol    ("基本色", Color) = (0.5, 0.5, 0.5, 1.0)
            _LightColor ("LightColor", Color) = (1,1,1,1)
            _EnvDiffInt ("环境漫反射强度", Range(0, 1)) = 0.2
            _EnvTopCol  ("顶部颜色", Color) = (1.0, 1.0, 1.0, 1.0)
            _EnvMidCol  ("中部颜色", Color) = (1.0, 1.0, 1.0, 1.0)
            _EnvBtmCol  ("底部颜色", Color) = (0.0, 0.0, 0.0, 0.0)
        [Header(Specular)]
            _SpecPow    ("高光次幂", Range(1, 90)) = 30
            _EnvSpecInt ("环境镜面反射强度", Range(0, 5)) = 0.2
            _FresnelPow ("菲涅尔次幂", Range(0, 5)) = 1
            _CubemapMip ("环境球Mip", Range(1, 7)) = 1
        [Header(Emission)]
            _EmitInt    ("自发光强度", range(1, 10)) = 1

    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        LOD 100
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            // 引入文件
            #include "AutoLight.cginc"                              // 投影必须依赖
            #include "Lighting.cginc"                               // 投影必须依赖
            #pragma multi_compile_fwdbase_fullshadows
            #pragma multi_compile_fog
            #pragma target 3.0

            
            // 输入参数
            uniform sampler2D _MainTex;
            uniform sampler2D _NormTex;
            uniform sampler2D _SpecTex;
            uniform sampler2D _EmitTex;
            uniform samplerCUBE _Cubemap;
            
            uniform float3 _MainCol;
            uniform float3 _LightColor;
            uniform float _EnvDiffInt;
            uniform float3 _EnvTopCol;
            uniform float3 _EnvMidCol;
            uniform float3 _EnvBtmCol;

            uniform float _SpecPow;
            uniform float _EnvSpecInt;
            uniform float _FresnelPow;
            uniform float _CubemapMip;
            uniform float _EmitInt;

            

            struct VertexInput {
                float4 vertex :     POSITION;
                float2 uv0 :        TEXCOORD0;
                float4 normal :     NORMAL;         // 法线
                float4 tangent :    TANGENT;       // 切线
            };
            struct VertexOutput {
                float4 pos   :  SV_POSITION;                        // 屏幕顶点位置
                float2 uv0   :  TEXCOORD0;
                float4 posWS :  TEXCOORD1;                          // 世界空间顶点
                float3 nDirWS:  TEXCOORD2;                          // 世界空间法线
                float3 tDirWS:  TEXCOORD3;                          // 世界空间切线
                float3 bDirWS:  TEXCOORD4;                          // 世界空间副切线
                LIGHTING_COORDS(5,6)                               // 投影坐标
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;                   // 新建输出结构
                    o.pos = UnityObjectToClipPos( v.vertex);        // 顶点位置 os > cs
                    o.uv0 = v.uv0;                                  // 传递法线
                    o.posWS = mul(unity_ObjectToWorld, v.vertex);   // 顶点位置 os > ws
                    o.nDirWS = UnityObjectToWorldNormal( v.normal); // 法线方向 os > ws
                    o.tDirWS = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0.0)).xyz); //切线方向 os > ws
                    o.bDirWS = normalize(cross(o.nDirWS, o.tDirWS) * v.tangent.w); // 副切线方向
                    TRANSFER_VERTEX_TO_FRAGMENT(o);                 // 投影
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                // 向量准备
                float3 nDirTS = UnpackNormal(tex2D(_NormTex, i.uv0)).rgb; 
                float3x3 TBN = float3x3(i.tDirWS, i.bDirWS, i.nDirWS);
                float3 nDirWS = normalize(mul(nDirTS, TBN));
                float3 vDirWS = normalize(_WorldSpaceCameraPos.xyz - i.posWS.xyz);
                float3 vrDirWS = reflect(-vDirWS, nDirWS);
                float lDirWS = _WorldSpaceLightPos0.xyz;
                float3 lrDirWS = reflect(-lDirWS, nDirWS);

                // 中间量
                float nDotl = dot(nDirWS, lDirWS);
                float vDotr = dot(vDirWS, lrDirWS);
                float vDotn = dot(vDirWS, nDirWS);
                
                // 纹理采样
                float4 var_MainTex = tex2D(_MainTex, i.uv0);
                float4 var_SpecTex = tex2D(_SpecTex, i.uv0);
                float3 var_EmitTex = tex2D(_EmitTex, i.uv0).rgb;
                float cubemapMip = lerp(_CubemapMip, 1.0, var_SpecTex.a);
                float3 var_Cubemap = texCUBElod(_Cubemap, float4(vrDirWS, cubemapMip));

                // 基本光照模型
                    // 光源漫反射
                    float3 baseCol = var_MainTex.rgb * _MainCol;
                    float lambert = max(0.0, nDotl);   
                    // 镜面反射
                    float sepcCol = var_SpecTex.rgb;
                    float specPow = lerp(1, _SpecPow, var_SpecTex.a);           // lerp 线性插值
                    float phong = pow(max(0.0, vDotr), specPow);   
                    // 光源反射混合
                    float shadow = LIGHT_ATTENUATION(i);    
                    float3 dirLighting = (_MainCol * lambert + sepcCol * phong) * _LightColor * shadow;
                    // 环境漫反射
                    float topMask = max(0.0, nDirWS.g);
                    float btmMask = max(0.0, -nDirWS.g);
                    float midMask = 1.0 - topMask - btmMask;
                    float3 envCol = _EnvTopCol * topMask + _EnvMidCol * midMask + _EnvBtmCol * btmMask;
                    float3 envDiff = baseCol * envCol * _EnvSpecInt;
                    // 环境镜面反射
                    float fresnel = pow(max(0.0, 1.0 - vDotn), _FresnelPow);    // 菲涅尔
                    float3 envSpec = var_Cubemap * fresnel * _EnvSpecInt;
                    // 环境反射混合
                    float occlusion = var_MainTex.a;    // 环境闭塞
                    float3 envLighting = (envDiff + envSpec) * occlusion;
                    // 自发光
                    float3 emission = var_EmitTex * _EmitInt;
                    // 最终混合
                    float3 finalRGB = dirLighting + envLighting + emission;
                    
                    return float4(finalRGB, 1.0);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}
